home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/env python
-
- # MintDisk
- # No Copyright (What for?) Clem <root@linuxmint.com>
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; Version 2
- # of the License.
-
- try:
- import os
- import commands
- import sys
- import string
- except Exception, detail:
- print detail
- pass
-
-
- # prepare the log
- log = open("/usr/lib/linuxmint/mintDisk/mintDisk.log", "w")
-
- # checking os-prober
- log.writelines("-------------------------\n")
- log.writelines("* checking the presence of os-prober...\n")
- if (commands.getoutput("which os-prober") == "/usr/bin/os-prober"):
- log.writelines(" -- os-prober is present, exiting\n")
- sys.exit()
- else:
- log.writelines(" -- os-prober is absent\n")
-
- # prepare the environment
- log.writelines("-------------------------\n")
- log.writelines("* checking system environment...\n")
- user = os.environ.get("USER")
- home = os.environ.get("HOME")
- locale = os.environ.get("LANG")
- log.writelines(" -- user = " + user + "\n")
- log.writelines(" -- home = " + home + "\n")
- log.writelines(" -- locale = " + locale + "\n")
-
- # configure options
- log.writelines("-------------------------\n")
- log.writelines("* configuring options...\n")
- lnMountPoint = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/options | grep MOUNT_POINT")
- mountPointArray = string.split(lnMountPoint, "=")
- mountPoint = string.strip(mountPointArray[1])
- mountPoint = string.replace(mountPoint, "<<HOME>>", home)
- log.writelines(" -- mount point: " + mountPoint + "\n")
- lnShowLinks = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/options | grep SHOW_LINKS")
- showLinksArray = string.split(lnShowLinks, "=")
- showLinks = string.strip(showLinksArray[1])
- if showLinks == "1":
- log.writelines(" -- show links: Yes\n")
- else:
- log.writelines(" -- show links: No\n")
- lnLinksLocation = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/options | grep LINKS_LOCATION")
- linksLocationArray = string.split(lnLinksLocation, "=")
- linksLocation = string.strip(linksLocationArray[1])
- linksLocation = string.replace(linksLocation, "<<HOME>>", home)
- log.writelines(" -- links location: " + linksLocation + "\n")
- log.writelines("-------------------------\n")
- # read the configuration
- fat32_supported = "0"
- fat32_mount_type = ""
- fat32_mount_options = ""
- fat32_type = "b"
- fat32_LBA_supported = "0"
- fat32_LBA_mount_type = ""
- fat32_LBA_mount_options = ""
- fat32_LBA_type = "c"
- ntfs_supported = "0"
- ntfs_mount_type = ""
- ntfs_mount_options = ""
- ntfs_type = "7"
- config_file = open("/usr/lib/linuxmint/mintDisk/mount_options")
- for config_item in config_file.readlines():
- array_item = string.split(config_item)
- if (len(array_item) == 4):
- (config_type, config_supported, config_mount_type, config_mount_options) = string.split(config_item)
- if (fat32_type == config_type):
- log.writelines("* loading support for FAT32...\n")
- fat32_supported = config_supported
- fat32_mount_type = config_mount_type
- fat32_mount_options = config_mount_options
- log.writelines(" -- partition type = " + fat32_type + "\n")
- log.writelines(" -- mount type = " + fat32_mount_type + "\n")
- log.writelines(" -- mount options = " + fat32_mount_options + "\n")
- elif (fat32_LBA_type == config_type):
- log.writelines("* loading support for FAT32 (LBA)...\n")
- fat32_LBA_supported = config_supported
- fat32_LBA_mount_type = config_mount_type
- fat32_LBA_mount_options = config_mount_options
- log.writelines(" -- partition type = " + fat32_LBA_type + "\n")
- log.writelines(" -- mount type = " + fat32_LBA_mount_type + "\n")
- log.writelines(" -- mount options = " + fat32_LBA_mount_options + "\n")
- elif (ntfs_type == config_type):
- log.writelines("* loading support for NTFS...\n")
- ntfs_supported = config_supported
- ntfs_mount_type = config_mount_type
- ntfs_mount_options = config_mount_options
- log.writelines(" -- partition type = " + ntfs_type + "\n")
- log.writelines(" -- mount type = " + ntfs_mount_type + "\n")
- log.writelines(" -- mount options = " + ntfs_mount_options + "\n")
-
- # remove existing links
- log.writelines("-------------------------\n")
- log.writelines("* removing existing links (if any)...\n")
- status = commands.getoutput("rm " + home + "/mintDisk*.desktop")
- status = commands.getoutput("rm " + linksLocation + "mintDisk*.desktop")
-
- # See what partitions are available
- log.writelines("-------------------------\n")
- log.writelines("* scanning partitions...\n")
- os.system("fdisk -l > /usr/lib/linuxmint/mintDisk/detected_partitions")
- detected_partitions_file = open("/usr/lib/linuxmint/mintDisk/detected_partitions")
- for detected_partition in detected_partitions_file.readlines():
- try:
- if (string.find(detected_partition, "/dev/") == 0):
- array = string.split(detected_partition)
- device = array[0]
- type = array[4]
- size = array[3]
- if (array[1] == "*"):
- type = array[5]
- size = array[4]
- size = string.replace(size, "+", "")
- size = int(size)/1000000
- strSize = str(size) + "GB"
- log.writelines(" -- detected device "+ device + " of type " + type + " and size " + strSize + "\n")
- mount_type = ""
- mount_options = ""
- device_type = "none"
- if (fat32_type == type and fat32_supported == "1"):
- device_type = "FAT32"
- mount_type = fat32_mount_type
- mount_options = fat32_mount_options
- elif (fat32_LBA_type == type and fat32_LBA_supported == "1"):
- device_type = "FAT32(LBA)"
- mount_type = fat32_LBA_mount_type
- mount_options = fat32_LBA_mount_options
- elif (ntfs_type == type and ntfs_supported == "1"):
- device_type = "NTFS"
- mount_type = ntfs_mount_type
- mount_options = ntfs_mount_options
-
- if (device_type != "none"):
- device_name = string.replace(device, "/dev/", "")
- mount_point = string.replace(mountPoint, "<<DEVICE>>", device_name)
-
- # find alias for the device
- alias = strSize + " " + device_type + " Volume (" + device_name + ")"
- nbAliases = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/aliases | grep " + device + " | wc -l")
- if (nbAliases == "1"):
- aliasStr = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/aliases | grep " + device)
- aliasArray = string.split(aliasStr, "=")
- if (len(aliasArray)==2):
- alias = aliasArray[1]
- alias = string.lstrip(alias)
- log.writelines(" => alias: " + alias + "\n")
- mount_point = string.replace(mount_point, "<<ALIAS>>", alias)
- else:
- log.writelines(" => no alias defined\n")
- mount_point = string.replace(mount_point, "<<ALIAS>>", device_name)
-
- # check to see if this is not taken care by fstab...
- managed_by_fstab = commands.getoutput("cat /etc/fstab | grep " + device + " | wc -l")
- if (managed_by_fstab == "0"):
- # mounting the device if not already mounted
- alreadyMounted = commands.getoutput("mount | grep " + device + " | wc -l")
- if (alreadyMounted == "0"):
- # mounting the device
- log.writelines(" => mounting partition...\n")
- status = commands.getoutput("mkdir -p " + mount_point)
- status = commands.getoutput("mount -t " + mount_type + " -o " + mount_options + " " + device + " " + mount_point)
- log.writelines(" => " + status + "\n")
- else:
- log.writelines(" => partition mounted already\n")
-
- alreadyMounted = commands.getoutput("mount | grep " + device + " | wc -l")
- if (alreadyMounted == "0"):
- log.writelines(" => could not mount partition, no link created\n")
- else:
- if (showLinks == "1"):
- # get the mount point
- real_mount_point_line = commands.getoutput("mount | grep " + device )
- real_mount_point_array = real_mount_point_line.split()
- real_mount_point = real_mount_point_array[2]
-
- # creating a link to the mount point
- log.writelines(" => creating a link to " + real_mount_point + "\n")
- linkPath = linksLocation + "mintDisk_" + device_name + ".desktop"
- try:
- link = open(linkPath, "w")
- except Exception, detail:
- os.system("mkdir -p " + linksLocation)
- os.system("chown " + user + ":" + user + " " + linksLocation)
- link = open(linkPath, "w")
- link.writelines("[Desktop Entry]\n")
- link.writelines("Encoding=UTF-8\n")
- link.writelines("Version=1.0\n")
- link.writelines("Type=Link\n")
- link.writelines("URL=" + real_mount_point + "\n")
- link.writelines("Name=" + alias + "\n")
- link.writelines("Comment=" + strSize + " " + device_type + " Volume (" + device_name + ")\n")
- link.writelines("Icon=/usr/share/icons/gnome/scalable/devices/drive-harddisk.svg\n")
- link.close()
- os.system("chown " + user + ":" + user + " " + linkPath)
- else:
- log.writelines(" => no link created\n")
- else:
- log.writelines(" => partition managed by /etc/fstab\n")
- else:
- log.writelines(" => partition type not supported\n")
- except Exception, detail:
- print "ERROR"
- log.writelines(detail)
-